package com.wzdigit.wms.wms.controller.outbound;


import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.basic.annotation.FormLock;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.picktask.PicktaskResp;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.outbound.PicktaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 拣货_PDA 前端控制器
 * </p>
 *
 * @author yangwenbiao
 * @since 2021-02-26
 */

@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "拣货_PDA")
public class OrderPickController {

    public static final String REQUEST_MODEL = "/order_pick_pda";

    @Autowired
    PicktaskService picktaskService;


    @ApiOperation(value = "查询子任务信息")
    @GetMapping(value = REQUEST_MODEL + "/getTaskInfo/{picktaskDtlId}/{picktaskLineNo}")
    public Result<PicktaskResp> getTaskInfo(@ApiParam("拣货任务明细ID") @PathVariable("picktaskDtlId") Integer picktaskDtlId,
                                            @ApiParam("拣货任务行号") @PathVariable("picktaskLineNo") Integer picktaskLineNo,
                                            HttpServletRequest servletRequest,
                                            HttpServletResponse servletResponse) throws Exception {
        try {
            PicktaskResp picktaskResp = picktaskService.getTaskInfo(picktaskDtlId, picktaskLineNo, servletRequest, servletResponse);
            Result<PicktaskResp> result = new Result<>();

            result.setData(picktaskResp);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    //TODO 加个锁（ZYF)
    @FormLock(isdeal = true)
    @ApiOperation(value = "扫描SN")
    @GetMapping(value = REQUEST_MODEL + "/scan_sn")
//    @PostMapping(value = REQUEST_MODEL + "/scan_sn/{picktaskDtlId}/{sn}/{scanMode}")
    public Result<Integer> scanSn(@RequestParam @ApiParam("拣货任务明细ID") Integer picktaskDtlId,
                                  @RequestParam @ApiParam("SN") String sn,
                                  @RequestParam @ApiParam("拣货类型——0:单台，1:整板") Integer scanMode,
                                  HttpServletRequest servletRequest,
                                  HttpServletResponse servletResponse) throws Exception {
        try {
            Result<Integer> result = new Result<>();
            Integer picktaskCount = picktaskService.scanSn(picktaskDtlId, sn, scanMode, servletRequest, servletResponse);
            result.setData(picktaskCount);
            return result;
        } catch (RestStatusException ex) {
//            RedisUtil.delete(key);
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

}